package com.sise.wangzhan;

import com.netflix.loadbalancer.BaseLoadBalancer;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.PingUrl;
import com.netflix.loadbalancer.Server;

import java.util.ArrayList;
import java.util.List;

/**
 * @Classname ChoseServerTest
 * @Description TODO
 * @Date 2020/3/26 11:03
 * @Created by wangzhan
 */
public class ChoseServerTest {

    public static void main(String[] args) {

        //负载均衡器
        //ILoadBalancer Ib = new BaseLoadBalancer();//使用默认的负载均衡规则RoundRobinRule（轮询策略

        //测试类中加载自己的负载均衡策略
        BaseLoadBalancer Ib = new BaseLoadBalancer();
        Ib.setRule(new MyRule(Ib));

        List<Server> servers = new ArrayList<Server>();
        servers.add(new Server("localhost", 8081));
        servers.add(new Server("localhost", 8085));
        Ib.addServers(servers);




        for (int i = 0; i < 10; i++) {
            Server server = Ib.chooseServer(null);
            System.out.println(server);
        }
        /*Ib.setPing(new PingUrl());
        Ib.setPingInterval(2);


        for (Server s:Ib.getAllServers()) {
            System.out.println(s.getHostPort()+"   State:"+s.isAlive());
        }*/

    }


}
